package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.Customer;
import cn.wolfcode.crm.domain.Employee;
import cn.wolfcode.crm.domain.SystemDictionaryItem;
import cn.wolfcode.crm.query.CustomerQueryObject;
import cn.wolfcode.crm.query.PageResult;
import cn.wolfcode.crm.service.ICustomerService;
import cn.wolfcode.crm.service.IRoleService;
import cn.wolfcode.crm.service.ISystemDictionaryItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

//类似之前的Servlet
//请求用户的请求和相应

@Controller//IOC注解，将Controller交给Spring容器管理
@RequestMapping("/customer")
public class CustomerController {

    //自动去Spring容器中找到对应类型或者名称的对象并设置给当前字段
    @Autowired
    private ICustomerService service;
    @Autowired
    private IRoleService roleService ;
    @Autowired
    private ISystemDictionaryItemService systemDictionaryItemService ;

    //处理查询的请求
    //查询潜在客户
    @RequestMapping("/potentialList")
    public String list(Model model, @ModelAttribute("qo") CustomerQueryObject qo, HttpSession session) {
        //查询到所有的部门
        //设置潜在客户的状态
        qo.setStatus(0);

        //判断用户是否是超管或经理
        //用户在登录成功之后共享在了Session作用域中
        Employee employee = (Employee) session.getAttribute("EMPLOYEE_IN_SESSION");

        //查询当前用户拥有的角色
        List<String> sns = roleService.listAllRoleSnByEmpId(employee.getId());
        PageResult result = null;
        if (employee.isAdmin() || sns.contains("Market_Manager")) {
            result = service.query(qo);
        } else {
            //将当前员工的id作为销售员的id
            qo.setSellerId(employee.getId());
            result = service.query(qo);
        }



        //将查询到的部门共享给页面
        model.addAttribute("result", result);



        //查询所有的市场人员
        List<Employee> sellers = service.listAllSellers("Market","Market_Manager");
        model.addAttribute("sellers", sellers);

        //查询所有的工作
        List<SystemDictionaryItem> jobs = systemDictionaryItemService.listByDicSn("job");
        model.addAttribute("jobs", jobs);
        //查询所有的来源
        List<SystemDictionaryItem> sources = systemDictionaryItemService.listByDicSn("source");
        model.addAttribute("sources", sources);

        //跟进方式
        List<SystemDictionaryItem> communicationMethods = systemDictionaryItemService.listByDicSn("communicationMethod");
        model.addAttribute("communicationMethods", communicationMethods);


        //指定页面跳转的视图
        return "customer/list";
    }


    @RequestMapping("/input")
    public String input(Long id, Model model) {
        if (id != null) {
            Customer dept = service.get(id);
            model.addAttribute("entity", dept);
        }
        //跳转到input.jsp页面
        return "customer/input";
    }

    //执行保存或更新
    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(Customer c, HttpSession session) {
        if (c.getId() != null) {
            //更新
            service.update(c);
        } else {
            //保存
            //封装客户的录入人员、录入时间
            Employee employee = (Employee) session.getAttribute("EMPLOYEE_IN_SESSION");
            c.setInputUser(employee);

            c.setInputTime(new Date());
            service.save(c);
        }

        return "redirect:/customer/potentialList.do";
    }

    //执行保存或更新
    @RequestMapping("/updateStatus")
    public String updateStatus(Long customerId, Integer status) {
        service.updateStatus(customerId,status);

        return "redirect:/customer/potentialList.do";
    }
}
